package com.moralesce.gt.util;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;

/**
 * Provides utility methods for File instances.
 * 
 * @author Brandon Grenier
 */
public final class FileUtil {

	private FileUtil() {

	}

	/**
	 * Opens the file with the given <em>filename</em>
	 * 
	 * This method specifically looks for files residing in the classpath.
	 * 
	 * @param filename The name of the file to open.
	 * 
	 * @return The file identified by the given name.
	 */
	public static File open(String filename) {
		try {
			File file = File.createTempFile("temp", null);
			file.deleteOnExit();
			FileUtils.copyURLToFile(FileUtil.class.getClassLoader().getResource(filename), file);
			return file;
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Reads the contents of the file identified by the specified
	 * <em>filename</em> line by line into a list of strings.
	 * 
	 * This method specifically looks for files residing in the classpath. The
	 * file is always closed.
	 * 
	 * @param filename The filename of the file to get content for.
	 * 
	 * @return A List<String> lines representing the contents of the file
	 * identified by the specified <em>filename</em>.
	 */
	public static List<String> readLines(String filename) {
		try {
			return FileUtils.readLines(open(filename));
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
